package com.example.aopdemo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy implements InvocationHandler {
    private Object delegate;

    public Object bind(Object delegate) {
        this.delegate = delegate;
        return Proxy.newProxyInstance(this.delegate.getClass().getClassLoader(), this.delegate.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        try {
            // 织入逻辑
            System.out.println("问候之前的日志记录...");
            result = method.invoke(this.delegate, args);
            // 织入逻辑
            System.out.println("问候之后的日志记录...");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
